Skip to content

Base Reward class

Bases: SynalinksSaveable

Per-sample reward base; subclass it unless a score needs the whole batch.

This is the class to subclass in order to create new custom rewards.

Parameters:

Name Type Description Default
name str

Optional name for the reward instance.

None
reduction str

Optional. One of "mean", "sum", "min", "max", "none" or None. Applied by __call__ when invoked on a batch directly (standalone evaluation) and propagated through compile to control how the trainer/optimizer reduce per-sample rewards into the scalar shown in progress logs and used for candidate scoring. Use "min" to score by the worst sample (robust/pessimistic) or "max" for the best (optimistic / best-of-N). "none"/ None falls back to "mean" for those scalar consumers (per-sample values are always preserved for the optimizer's RL bookkeeping).

'mean'
in_mask list

Optional. List of exact field names to keep before computing the reward.

None
out_mask list

Optional. List of exact field names to drop before computing the reward.

None
in_mask_pattern str

Optional. Regex pattern; fields whose names match are kept (combined with in_mask via OR).

None
out_mask_pattern str

Optional. Regex pattern; fields whose names match are dropped (combined with out_mask via OR).

None

To be implemented by subclasses:

  • call(): Contains the logic for eval calculation using y_true, y_pred. A call() that also declares an x argument (or takes **kwargs) receives the program's inputs, unmasked (see mask), or None when they are not given.
Source code in synalinks/src/rewards/reward.py
@synalinks_export(["synalinks.Reward", "synalinks.rewards.Reward"])
class Reward(SynalinksSaveable):
    """Per-sample reward base; subclass it unless a score needs the whole batch.

    This is the class to subclass in order to create new custom rewards.

    Args:
        name (str): Optional name for the reward instance.
        reduction (str): Optional. One of ``"mean"``, ``"sum"``, ``"min"``,
            ``"max"``, ``"none"`` or ``None``. Applied by ``__call__`` when
            invoked on a batch directly (standalone evaluation) and
            propagated through ``compile`` to control how the
            trainer/optimizer reduce per-sample rewards into the scalar
            shown in progress logs and used for candidate scoring. Use
            ``"min"`` to score by the worst sample (robust/pessimistic) or
            ``"max"`` for the best (optimistic / best-of-N). ``"none"``/
            ``None`` falls back to ``"mean"`` for those scalar consumers
            (per-sample values are always preserved for the optimizer's RL
            bookkeeping).
        in_mask (list): Optional. List of exact field names to keep before
            computing the reward.
        out_mask (list): Optional. List of exact field names to drop before
            computing the reward.
        in_mask_pattern (str): Optional. Regex pattern; fields whose names
            match are kept (combined with ``in_mask`` via OR).
        out_mask_pattern (str): Optional. Regex pattern; fields whose names
            match are dropped (combined with ``out_mask`` via OR).

    To be implemented by subclasses:

    * `call()`: Contains the logic for eval calculation using `y_true`,
        `y_pred`. A `call()` that also declares an `x` argument (or takes
        `**kwargs`) receives the program's inputs, unmasked (see `mask`), or
        `None` when they are not given.
    """

    def __init__(
        self,
        name=None,
        description=None,
        reduction="mean",
        in_mask=None,
        out_mask=None,
        in_mask_pattern=None,
        out_mask_pattern=None,
    ):
        self.name = name or auto_name(self.__class__.__name__)
        self.description = docstring_summary(self) if description is None else description
        self.reduction = standardize_reduction(reduction)
        self.in_mask = in_mask
        self.out_mask = out_mask
        self.in_mask_pattern = in_mask_pattern
        self.out_mask_pattern = out_mask_pattern

    async def __call__(self, y_true, y_pred, x=None):
        with ops.name_scope(self.name):
            y_true, y_pred, x = self.mask(y_true, y_pred, x)
            if accepts_argument(self.call, "x"):
                rewards = await self.call(y_true, y_pred, x=x)
            else:
                rewards = await self.call(y_true, y_pred)
            return reduce_values(
                rewards,
                reduction=self.reduction,
            )

    def mask(self, y_true, y_pred, x=None):
        """Apply this reward's masks: to `y_true` and `y_pred` only.

        The program's inputs `x` are converted but never masked, so a keep-list
        of output fields cannot drop the question a reward needs.

        Returns:
            (tuple): The masked `y_true` and `y_pred`, and `x`.
        """
        y_true, y_pred = apply_masks(
            y_true,
            y_pred,
            in_mask=self.in_mask,
            in_mask_pattern=self.in_mask_pattern,
            out_mask=self.out_mask,
            out_mask_pattern=self.out_mask_pattern,
        )
        return y_true, y_pred, mask_data_models(x)

    async def call(self, y_true, y_pred):
        raise NotImplementedError

    async def close(self):
        """Release what this reward holds. Idempotent.

        Nothing, for most rewards; its program, for a judge.
        """

    def get_config(self):
        return {
            "name": self.name,
            "description": self.description,
            "reduction": self.reduction,
            "in_mask": self.in_mask,
            "out_mask": self.out_mask,
            "in_mask_pattern": self.in_mask_pattern,
            "out_mask_pattern": self.out_mask_pattern,
        }

    @classmethod
    def from_config(cls, config):
        return cls(**config)

close() async

Release what this reward holds. Idempotent.

Nothing, for most rewards; its program, for a judge.

Source code in synalinks/src/rewards/reward.py
async def close(self):
    """Release what this reward holds. Idempotent.

    Nothing, for most rewards; its program, for a judge.
    """

mask(y_true, y_pred, x=None)

Apply this reward's masks: to y_true and y_pred only.

The program's inputs x are converted but never masked, so a keep-list of output fields cannot drop the question a reward needs.

Returns:

Type Description
tuple

The masked y_true and y_pred, and x.

Source code in synalinks/src/rewards/reward.py
def mask(self, y_true, y_pred, x=None):
    """Apply this reward's masks: to `y_true` and `y_pred` only.

    The program's inputs `x` are converted but never masked, so a keep-list
    of output fields cannot drop the question a reward needs.

    Returns:
        (tuple): The masked `y_true` and `y_pred`, and `x`.
    """
    y_true, y_pred = apply_masks(
        y_true,
        y_pred,
        in_mask=self.in_mask,
        in_mask_pattern=self.in_mask_pattern,
        out_mask=self.out_mask,
        out_mask_pattern=self.out_mask_pattern,
    )
    return y_true, y_pred, mask_data_models(x)